package cc.cix.gt.common.util;

import cc.cix.gt.common.entity.QueryRequest;
import cc.cix.gt.common.enums.SortType;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 处理排序工具类
 *
 * @author cix
 */
public abstract class SortUtil {

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request            QueryRequest
     * @param wrapper            wrapper
     * @param defaultSortField   默认排序的字段
     * @param defaultOrder       默认排序的规则
     * @param defaultFilterField 默认过滤时间范围字段
     * @param camelToUnderscore  是否开启驼峰转下划线
     */
    public static void handleWrapperSort(QueryRequest request,
                                         QueryWrapper<?> wrapper, String defaultSortField,
                                         String defaultOrder, String defaultFilterField,
                                         boolean camelToUnderscore) {
        String sortField = StrUtil.isNotBlank(sortField = request.getSortField()) ?
                sortField : defaultSortField;
        String order = StrUtil.isNotBlank(order = request.getOrder()) ? order : defaultOrder;
        String filterField = StrUtil.isNotBlank(filterField = request.getFilterField()) ?
                filterField : defaultFilterField;

        // 驼峰转下划线
        if (camelToUnderscore) {
            sortField = SystemUtil.camelToUnderscore(sortField);
            filterField = SystemUtil.camelToUnderscore(filterField);
        }

        // 字段排序
        if (StrUtil.isNotBlank(sortField) && StrUtil.isNotBlank(order)) {
            if (StrUtil.equals(order, SortType.ORDER_DESC.getValue())) {
                wrapper.orderByDesc(sortField);
            } else {
                wrapper.orderByAsc(sortField);
            }
        }

        // 时间范围过滤
        wrapper.ge(ObjectUtil.isNotNull(request.getStartTime()), filterField, request.getStartTime())
                .le(ObjectUtil.isNotNull(request.getEndTime()), filterField, request.getEndTime());
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param wrapper wrapper
     */
    public static void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper) {
        handleWrapperSort(request, wrapper, "createTime",
                SortType.ORDER_DESC.getValue(),
                "createTime", true);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param wrapper wrapper
     */
    public static void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper,
                                         String defaultFilterField) {
        handleWrapperSort(request, wrapper, "createTime",
                SortType.ORDER_DESC.getValue(), defaultFilterField, true);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param wrapper wrapper
     */
    public static void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper,
                                         String defaultSortField, String defaultOrder) {
        handleWrapperSort(request, wrapper, defaultSortField, defaultOrder,
                "createTime", true);
    }


}
